<?php
	session_start();          // 为什么注销这个呢，因为sidebar.php 已经有session_start();了 这个开启会有warning.
	//$_SESSION["hit"] = 1;			//设置点击标志，没访问过之前为1；访问过后unset;
	unset($_SESSION["hit"]);		//用这样判断似乎更好？ 从链接访问的时候都是unset();
	include_once("conn.php");
	
	$get_board = $_GET["board"];
	
	$sql = "select * from bbs_session where s_name = '".$get_board."'";
	$result = mysql_query($sql);
	$q_result = mysql_fetch_array($result);
	if($q_result["s_id"])
	{
		$board_name = $get_board;				//版面名
		$board_admin_name = $q_result["s_admin_name"];		//版主名

		$sql = "select count(t_id) as t_count from bbs_topic where t_session_name = '".$board_name."'";
		$result = mysql_query($sql);
		$q_result = mysql_fetch_array($result);
		$t_s_count = $q_result["t_count"];			//版面的主帖总数


		$sql_p = "select s_profile from bbs_session where s_name = '".$board_name."'";
		$result_p = mysql_query($sql_p);
		$q_result_p = mysql_fetch_array($result_p);
		$board_profile = $q_result_p["s_profile"];				//版面寄语
		

	}
	else 
	{
		echo "版面不存在，请创建版面，或者检查链接<a>";
	}

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<script>

function $(id) {
	return document.getElementById(id);
}
function insertsmiley(icon) {  
  $('word').value+= ':'+icon+':';   
}
</script>
<link rel = "stylesheet" href = "board.css" style = "text/css" />
<link rel = "stylesheet" href = "jiaobu.css" style = "text/css" />
<link rel = "stylesheet" href = "sidebar.css" style = "text/css" />
<link rel = "stylesheet" href = "board_header.css" style = "text/css" />

</head>
<body>
<div class = "header">
	<h4><a href = "#"><?php echo $board_name; ?></a> -- 讨论区 版主:<a href = "profile.php?uname=<?php echo $board_admin_name; ?>"><?php echo $board_admin_name;  ?></a></h3>
	<p id ="fabiao">本讨论区共有帖子<strong><?php echo $t_s_count; ?></strong>篇  &nbsp&nbsp&nbsp&nbsp<a href="public.php?board=<?php echo $board_name; ?>"><span id = "fb">发表主题</span></a></p>
	<p id = "profile"><strong>版面公告</strong>:<?php echo $board_profile; ?>
	<?php
	if(isset($_SESSION["current_user"]))
	{
		if($_SESSION["current_user"] == $board_admin_name)
		{
	?>
		<input type = "button" value = "编辑公告" id = "edit_profile">
	<?php } } ?>
	</p>
	<hr />
</div>

<div class = "body">
	<?php include_once("sidebar.php"); ?>
	
	<div class = "banmian">
		<table class = "tb_board">
			<tr>
				<th>作者</th>
				<th>发表时间</th>
				<th class = "bt">标题</th>
				<th id = "renqi">回帖/人气</th>
				<th>最新回复</th>
				<th>最后回复时间</th>
			</tr>
			
			<?php

				$page_size = 20;			//每页显示主题数目（版面）
				//$t_s_count  是版面主题数目
				$page_count = ceil($t_s_count/$page_size);		//最大页数，有的地方还要取余非0才用这句，取余为0时intval()为啥？
				if(!isset($_GET["page"]))
				{
					$page_num = 1;
				}
				else {
					$page_num = $_GET["page"];				//判断当前页码
				}

				$first = 1;
				$last = $page_count;
				$prev = $page_num -1;
				$next = $page_num +1;
				
				$offset = ($page_num-1)*$page_size;			//limit用到的
				
				//先输出置顶贴
				$zhiding_limit = 7;			//最大的置顶贴容纳量
				$sql = "select * from bbs_topic where t_state = 1 && t_session_name = '".$board_name."' order by t_time desc limit ".$zhiding_limit."";
				$result = mysql_query($sql);

				//以下这段为置顶贴的读取本贴信息操作。勿与下面的普通贴混合。内容一样的。不要改错！！
				
				while($q_result = mysql_fetch_array($result))
				{
				$author = $q_result["t_author"];
						$post_time = $q_result["t_time"];	//全名，含秒
						$post_time_hour = substr($post_time,0,16); //把秒去掉

						$post_title = $q_result["t_topic"];
						$post_id = $q_result["t_id"];				//帖子编号
						

						$r_sql = "select r_time,r_u_name from bbs_reply where r_tid = '".$post_id."' order by r_time desc limit 1";
						$r_result = mysql_query($r_sql);
						$q_r_result = mysql_fetch_array($r_result);

						$post_last_reply_time = $q_r_result["r_time"];		//最后回复时间
						$post_last_reply_name = $q_r_result["r_u_name"];		//最后回复人名字mysql 中的r_u_name 是回帖者名字,之前记错以为是主帖的人名，
						
						if($post_last_reply_name == null)				//从回帖表里找不到主帖的回帖，说明0回复，此时最新回复者/时间皆为本人和本主帖发表时间
						{
							$post_last_reply_name = $author;
							$post_last_reply_time = $post_time;

							$sql = "update bbs_topic set t_last_reply_time = '".$post_last_reply_time."' where t_id = '".$post_id."'";
							mysql_query($sql);
							$sql = "update bbs_topic set t_last_reply_name = '".$post_last_reply_name."' where t_id = '".$post_id."'";
							mysql_query($sql);
						}

						/*
						$post_last_reply_name = $q_result["t_last_reply_name"];		//最新回帖者
						$post_last_reply_time = $q_result["t_last_reply_time"];		//最新回贴时间
						*/



						$post_last_reply_hour = substr($post_last_reply_time,11,5);		//小时，分钟
						$post_last_reply_month = substr($post_last_reply_time,5,5);		//月，日

						$post_t_click_count = $q_result["t_click_count"];			//主题点击量
						
						//最后回复时间，从bbs_reply取
						//接下来count出这篇主帖的回复数是多少

						$sql2 = "select count(r_id) as r_count from bbs_reply where r_tid = '".$post_id."'";
						$result2 = mysql_query($sql2);
						$q_result2 = mysql_fetch_array($result2);
						$r_count = $q_result2["r_count"];			//回帖数



			//以上这段为置顶贴的读取本贴信息操作。勿与下面的普通贴混合。内容一样的。不要改错！！
						if(!isset($_GET["page"]))		//此句用来使置顶帖仅出现在首页
						{

					?>
					
					<tr>
						<td><a href = "profile.php?uname=<?php echo $author; ?>"><?php echo $author; ?></a></td>
						<td class = "td_time"><a><?php echo $post_time_hour;  ?></a></td>
						<td class = "td_bt"><img src = "photos/hot.gif" style="float:left;"><a class = "a_zhiding" href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $post_id;  ?>"><?php echo $post_title; ?></a></td>
						<td class = "td_renqi"><a><span style="color:red;"><?php echo $r_count; ?></span>/<span style="color:blue;"><?php echo $post_t_click_count; ?></span></a></td>
						<td><a href = "profile.php?uname=<?php echo $post_last_reply_name; ?>"><?php echo $post_last_reply_name; ?></a></td>
						<td><a><?php echo $post_last_reply_hour;echo "&nbsp".$post_last_reply_month; ?></a></td>
					</tr>
				<?php 
						}
						else if(isset($_GET["page"])) {
							if($_GET["page"] == 1){
										
				?>
								<tr>
									<td><a href = "profile.php?uname=<?php echo $author; ?>"><?php echo $author; ?></a></td>
									<td class = "td_time"><a><?php echo $post_time_hour;  ?></a></td>
									<td class = "td_bt"><img src = "photos/hot.gif" style="float:left;"><a class = "a_zhiding" href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $post_id;  ?>"><?php echo $post_title; ?></a></td>
									<td class = "td_renqi"><a><span style="color:red;"><?php echo $r_count; ?></span>/<span style="color:blue;"><?php echo $post_t_click_count; ?></span></a></td>
									<td><a href = "profile.php?uname=<?php echo $post_last_reply_name; ?>"><?php echo $post_last_reply_name; ?></a></td>
									<td><a><?php echo $post_last_reply_hour;echo "&nbsp".$post_last_reply_month; ?></a></td>
								</tr>
				<?php
							
							} //以上用来使置顶贴仅在首页出现。
							
						}
				?>
				<?php
				} //while的后半个括号 
				?>
				<!-- 先输出置顶贴 -->
				
			<?php
			$sql = "select * from bbs_topic where t_session_name = '".$board_name."' order by t_last_reply_time desc limit ".$offset.",".$page_size."";
				$result = mysql_query($sql);

				//上面这句和下面的判断post_state!=1 很重要，上面查出20条(含置顶贴)来显示，但下面的IF经过判断只显示了非置顶贴,加起来刚好仍然每页20贴，巧合	//以最多3置顶帖算，每页最多23贴(仅首页会出现这种情况)，最少17。	普遍情况都是正常的
				while($q_result = mysql_fetch_array($result))
				{
					$post_state = $q_result["t_state"];			//贴子是否置顶状态 置顶为1 ，没有则0
					if($post_state !=1)
					{
						$author = $q_result["t_author"];
						$post_time = $q_result["t_time"];	//全名，含秒
						$post_time_hour = substr($post_time,0,16); //把秒去掉

						$post_title = $q_result["t_topic"];
						$post_id = $q_result["t_id"];				//帖子编号
						

						$r_sql = "select r_time,r_u_name from bbs_reply where r_tid = '".$post_id."' order by r_time desc limit 1";
						$r_result = mysql_query($r_sql);
						$q_r_result = mysql_fetch_array($r_result);

						$post_last_reply_time = $q_r_result["r_time"];		//最后回复时间
						$post_last_reply_name = $q_r_result["r_u_name"];		//最后回复人名字mysql 中的r_u_name 是回帖者名字,之前记错以为是主帖的人名，
						
						if($post_last_reply_name == null)				//从回帖表里找不到主帖的回帖，说明0回复，此时最新回复者/时间皆为本人和本主帖发表时间
						{
							$post_last_reply_name = $author;
							$post_last_reply_time = $post_time;

							$sql = "update bbs_topic set t_last_reply_time = '".$post_last_reply_time."' where t_id = '".$post_id."'";
							mysql_query($sql);
							$sql = "update bbs_topic set t_last_reply_name = '".$post_last_reply_name."' where t_id = '".$post_id."'";
							mysql_query($sql);
						}

						/*
						$post_last_reply_name = $q_result["t_last_reply_name"];		//最新回帖者
						$post_last_reply_time = $q_result["t_last_reply_time"];		//最新回贴时间
						*/



						$post_last_reply_hour = substr($post_last_reply_time,11,5);		//小时，分钟
						$post_last_reply_month = substr($post_last_reply_time,5,5);		//月，日

						$post_t_click_count = $q_result["t_click_count"];			//主题点击量
						
						//最后回复时间，从bbs_reply取
						//接下来count出这篇主帖的回复数是多少

						$sql2 = "select count(r_id) as r_count from bbs_reply where r_tid = '".$post_id."'";
						$result2 = mysql_query($sql2);
						$q_result2 = mysql_fetch_array($result2);
						$r_count = $q_result2["r_count"];			//回帖数

					
					


			?>
			
			<tr>
				<td><a href = "profile.php?uname=<?php echo $author; ?>"><?php echo $author; ?></a></td>
				<td class = "td_time"><a><?php echo $post_time_hour;  ?></a></td>
				<td class = "td_bt"><a href = "browse.php?board=<?php echo $board_name; ?>&tid=<?php echo $post_id;  ?>"><?php echo $post_title; ?></a></td>
				<td class = "td_renqi"><a><span style="color:red;"><?php echo $r_count; ?></span>/<span style="color:blue;"><?php echo $post_t_click_count; ?></span></a></td>
				<td><a href = "profile.php?uname=<?php echo $post_last_reply_name; ?>"><?php echo $post_last_reply_name; ?></a></td>
				<td><a><?php echo $post_last_reply_hour;echo "&nbsp".$post_last_reply_month; ?></a></td>
			</tr>
			<?php
					}
				}

			?>			

		</table>
	</div>
</div>

<div class = "jiaobu">
	<table>
		<tr>
			<td>第<?php echo $page_num; ?>页</td>
		<?php
			if($page_count ==1)
			{
		?>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $first; ?>">首页</a></td>
		<?php
			}
			if($page_num>1 && $page_num<$page_count)
			{
		?>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $first; ?>">首页</a></td>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $prev; ?>">上一页</a></td>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $next; ?>">下一页</a></td>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $last; ?>">尾页</a></td>
		<?php
			}
			if($page_num == $page_count && $page_count>1)
			{
		?>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $first; ?>">首页</a></td>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $prev; ?>">上一页</a></td>				
		<?php
			}
			if($page_count>1 && $page_num == 1)
			{
		?>
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $next; ?>">下一页</a></td>				
				<td><a href = "page.php?board=<?php echo $board_name; ?>&page=<?php echo $last; ?>">尾页</a></td>
		<?php
			}
		?>
			<td><input type = "text" name = "page_num" id = "page_num" placeholder = "页数" /></td>
			<td><input type = "button" name = "" value = "跳到该页" /></td>
			<td></td>
			<td>精华区</td>
			<td>刷新</td>


	
		</tr>
	</table>
</div>

<!--
<div class = "footer">
	<?php include_once("jiaobu.php"); ?>
</div>
-->


</body>
</html>